<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\ApiController;
use App\Libraries\Error;
use App\Models\Insured;
use App\Models\User;
use Firebase\JWT\JWT;
use Illuminate\Http\Request;

class InsuredController extends ApiController
{


    public function create(Request $request) {
        if (!$request->token) {
            return showError(Error::ERR_ERROR, null, "无效的token!");
        }
        $userInfo = JWT::decode($request->token,env('JWT_KEY'), array('HS256'));
        $exp = $userInfo->exp;
        if (time() > $exp) {
            return showError(Error::ERR_TOKEN_ERROR);
        }
        $userInfo = (array)$userInfo->data;
        $uid = $userInfo['uid'];
        $user = User::where('uid',$uid)->where('status',1)->first();
        if (empty($user)) {
            return showError(Error::ERR_TOKEN_ERROR);
        }
//        $uid = '29221';
        $company_id = $userInfo['company_id'];

        if ( $request->id) {
            $insured = Insured::findOrFail($request->id);
            $insured->mtime = time();
        } else {
            $insured = new Insured();
            $insured->ctime = time();
        }
        $insured->employee_id = $request->employee_id;
        $insured->company_id = $company_id;
        $insured->scheme_id = $request->scheme_id;
        $insured->month = $request->month;
        $insured->city = $request->city;
        $insured->person_social_security = $request->person_social_security;
        $insured->company_social_security = $request->company_social_security;
        $insured->person_accumulation_fund = $request->person_accumulation_fund;
        $insured->company_accumulation_fund = $request->company_accumulation_fund;
        $insured->uid = $uid;

        $insured->save();

        return showError(Error::ERR_OK,['id'=>$insured->id]);

    }

    public function show(Request $request) {
        $insured = Insured::findOrFail($request->id);
        return showError(Error::ERR_OK,['insured'=>$insured]);
    }

    public function index(Request $request)
    {
        if (!$request->token) {
            return showError(Error::ERR_ERROR, null, "无效的token!");
        }
        $userInfo = JWT::decode($request->token,env('JWT_KEY'), array('HS256'));
        $exp = $userInfo->exp;
        if (time() > $exp) {
            return showError(Error::ERR_TOKEN_ERROR);
        }
        $userInfo = (array)$userInfo->data;
        $uid = $userInfo['uid'];
        $user = User::where('uid',$uid)->where('status',1)->first();
        if (empty($user)) {
            return showError(Error::ERR_TOKEN_ERROR);
        }
//        $uid = '29221';
        $company_id = $userInfo['company_id'];
        // 搜索内容
        $where = [];

        $where['company_id'] = $company_id;
        $where['status'] = 1;

        $list = Insured::getList($where);

        return showError(Error::ERR_OK,['list'=>$list]);
    }

    public function delete(Request $request) {
        Insured::where('id',$request->id)->update(['status'=>2,'mtime'=>time()]);
        return showError(Error::ERR_OK);
    }


}
